package com.gpnu7_412.yanzhi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpnu7_412.yanzhi.dto.ConciseUserDto;
import com.gpnu7_412.yanzhi.entity.*;
import com.gpnu7_412.yanzhi.exception.custom.DataNotFoundException;
import com.gpnu7_412.yanzhi.exception.custom.SQLExecuteException;
import com.gpnu7_412.yanzhi.mapper.*;
import com.gpnu7_412.yanzhi.service.HandingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author HJJ
 */
@Service
public class HandingServiceImpl implements HandingService {
    @Autowired
    HandingMapper handingMapper;
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    private UserHandingMapper userHandingMapper;
    @Autowired
    private DynamicHandingMapper dynamicHandingMapper;
    @Autowired
    private CommentHandingMapper commentHandingMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 获取所有用户举报信息
     * @param pageNum 查询页
     * @param pageSize 页面大小
     * @return 用户举报对象列表
     */
    @Override
    public PageInfo<UserHanding> getAllUserHanding(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserHanding> userHandingList = userHandingMapper.selectAll();
        PageInfo<UserHanding> pageInfo = new PageInfo<>(userHandingList, 5);
        return pageInfo;
    }

    /**
     * 获取所有动态举报信息
     * @param pageNum 查询页
     * @param pageSize 页面大小
     * @return 动态举报对象列表
     */
    @Override
    public PageInfo<DynamicHanding> getAllDynamicHanding(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DynamicHanding> dynamicHandingList = dynamicHandingMapper.selectAll();
        PageInfo<DynamicHanding> pageInfo = new PageInfo<>(dynamicHandingList, 5);
        return pageInfo;
    }

    /**
     * 获取所有评论举报信息
     * @param pageNum 查询页
     * @param pageSize 页面大小
     * @return 评论举报对象列表
     */
    @Override
    public PageInfo<CommentHanding> getAllCommentHanding(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CommentHanding> commentHandingList = commentHandingMapper.selectAll();
        PageInfo<CommentHanding> pageInfo = new PageInfo<>(commentHandingList, 5);
        return pageInfo;
    }

    /**
     * 同意用户举报
     *
     * @param handingId 用户举报ID
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean agreeUserHanding(Integer handingId) {
        if (handingId == null) {
            return false;
        }
        UserHanding userHanding = userHandingMapper.selectByHandingId(handingId);
        if (userHanding == null) {
            throw new DataNotFoundException("查无该用户举报信息");
        }
        userHandingMapper.deleteByHandingId(handingId);
        Notice notice = new Notice();
        notice.setUserId(userHanding.getUserId());
        notice.setType("用户");
        notice.setContent("您被举报，举报信息为：" + userHanding.getContent());
        noticeMapper.insertNotice(notice);
        return true;
    }

    /**
     * 同意动态举报
     *
     * @param handingId 动态举报ID
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean agreeDynamicHanding(Integer handingId) {
        if (handingId == null) {
            return false;
        }
        DynamicHanding dynamicHanding = dynamicHandingMapper.selectByHandingId(handingId);
        if (dynamicHanding == null) {
            throw new DataNotFoundException("查无该动态举报信息");
        }
        Dynamic dynamic = dynamicMapper.getDynamicById(dynamicHanding.getDynamicId());
        User user = userMapper.selectById(dynamic.getUserId());
        dynamicHandingMapper.deleteByHandingId(handingId);
        Notice notice = new Notice();
        notice.setUserId(user.getUserId());
        notice.setType("动态");
        notice.setContent("您发布的动态：" + dynamic.getTitle() + "被举报，举报理由为：" + dynamicHanding.getContent());
        noticeMapper.insertNotice(notice);
        return true;
    }

    /**
     * 同意评论举报
     *
     * @param handingId 评论举报ID
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean agreeCommentHanding(Integer handingId) {
        if (handingId == null) {
            return false;
        }
        CommentHanding commentHanding = commentHandingMapper.selectByHandingId(handingId);
        if (commentHanding == null) {
            throw new DataNotFoundException("查无该评论举报信息");
        }
        Comment comment = commentMapper.getCommentByCommentId(commentHanding.getCommentId());
        User user = userMapper.selectById(comment.getUserId());
        commentHandingMapper.deleteByHandingId(handingId);
        Notice notice = new Notice();
        notice.setUserId(user.getUserId());
        notice.setType("评论");
        notice.setContent("您的评论：" + comment.getContent() + "被举报，举报理由为：" + commentHanding.getContent());
        noticeMapper.insertNotice(notice);
        return true;
    }

    /**
     * 不同意用户举报
     *
     * @param handingId 用户举报ID
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disagreeUserHanding(Integer handingId) {
        if (handingId == null) {
            return false;
        }
        if (userHandingMapper.deleteByHandingId(handingId) != 1) {
            throw new SQLExecuteException("删除失败");
        }
        return true;
    }

    /**
     * 不同意动态举报
     *
     * @param handingId 动态举报ID
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disagreeDynamicHanding(Integer handingId) {
        if (handingId == null) {
            return false;
        }
        if (dynamicHandingMapper.deleteByHandingId(handingId) != 1) {
            throw new SQLExecuteException("删除失败");
        }
        return true;
    }

    /**
     * 不同意评论举报
     *
     * @param handingId 评论举报ID
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disagreeCommentHanding(Integer handingId) {
        if (handingId == null) {
            return false;
        }
        if (commentHandingMapper.deleteByHandingId(handingId) != 1) {
            throw new SQLExecuteException("删除失败");
        }
        return true;
    }



    //获取所有代办信息

    /**
     * @deprecated
     * @return
     */
    @Override
    public List<Handing> getHandingList() {
        List<Handing> handingList = handingMapper.selectAllHanding();
        if (handingList == null) {
            throw new SQLExecuteException("数据获取异常");
        }
        return handingList;
    }

    //处理代办信息

    /**
     * @deprecated
     * @param handle
     * @param handing
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleHanding(String handle, Handing handing){
        // 判断管理员处理结果
        if (handle.equals("驳回")) {
            Integer num = handingMapper.deleteHandingAccurate(handing);
            if (num <= 0) {
                throw new SQLExecuteException("删除操作失败");
            }
        } else if(handle.equals("受理")) {
            Notice notice = new Notice();
            notice.setUserId(handing.getUserId());
            notice.setType(handing.getType());
            notice.setContent(handing.getContent());
            Integer addNum = noticeMapper.insertNotice(notice);
            if (addNum <= 0) {
                throw new SQLExecuteException("添加操作失败");
            }
            Integer num = handingMapper.deleteHandingAccurate(handing);
            if (num <= 0) {
                throw new SQLExecuteException("删除操作失败");
            }
        }
    }
}
